<template>
  <div class="init-admin-container">
    <div class="init-admin-card">
      <div class="card-header">
        <h2>初始化管理员账户</h2>
        <p>首次使用系统，请创建管理员账户</p>
      </div>
      
      <div class="card-body">
        <el-form 
          ref="initForm" 
          :model="form" 
          :rules="rules" 
          label-width="100px"
          @submit.prevent="handleSubmit"
        >
          <el-form-item label="用户名" prop="username">
            <el-input 
              v-model="form.username" 
              placeholder="请输入管理员用户名"
              prefix-icon="el-icon-user"
            />
          </el-form-item>
          
          <el-form-item label="邮箱" prop="email">
            <el-input 
              v-model="form.email" 
              placeholder="请输入管理员邮箱"
              prefix-icon="el-icon-message"
            />
          </el-form-item>
          
          <el-form-item label="密码" prop="password">
            <el-input 
              v-model="form.password" 
              type="password" 
              placeholder="请输入管理员密码"
              prefix-icon="el-icon-lock"
              show-password
            />
          </el-form-item>
          
          <el-form-item label="确认密码" prop="confirmPassword">
            <el-input 
              v-model="form.confirmPassword" 
              type="password" 
              placeholder="请再次输入密码"
              prefix-icon="el-icon-lock"
              show-password
            />
          </el-form-item>
          
          <el-form-item>
            <el-button 
              type="primary" 
              :loading="loading" 
              @click="handleSubmit"
              class="submit-btn"
            >
              {{ loading ? '创建中...' : '创建管理员账户' }}
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import axios from 'axios'

export default {
  name: 'InitAdmin',
  data() {
    const validateConfirmPassword = (rule, value, callback) => {
      if (value !== this.form.password) {
        callback(new Error('两次输入的密码不一致'))
      } else {
        callback()
      }
    }
    
    return {
      loading: false,
      form: {
        username: '',
        email: '',
        password: '',
        confirmPassword: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 20, message: '用户名长度在 3 到 20 个字符', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' },
          { type: 'email', message: '请输入正确的邮箱格式', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 20, message: '密码长度在 6 到 20 个字符', trigger: 'blur' }
        ],
        confirmPassword: [
          { required: true, message: '请确认密码', trigger: 'blur' },
          { validator: validateConfirmPassword, trigger: 'blur' }
        ]
      }
    }
  },
  async mounted() {
    // 检查是否已有管理员
    await this.checkAdminExists()
  },
  methods: {
    async checkAdminExists() {
      try {
        const response = await axios.post('/api/admin/check-admin')
        if (response.data.success && response.data.data) {
          this.$message.warning('管理员账户已存在，正在跳转到登录页面...')
          setTimeout(() => {
            this.$router.push('/admin-login')
          }, 2000)
        }
      } catch (error) {
        // 检查管理员状态失败日志已移除
      }
    },
    
    handleSubmit() {
      this.$refs.initForm.validate(async (valid) => {
        if (valid) {
          this.loading = true
          try {
            const response = await axios.post('/api/admin/init', {
              username: this.form.username,
              email: this.form.email,
              password: this.form.password
            })
            
            if (response.data.success) {
              this.$message.success('管理员账户创建成功！正在跳转到登录页面...')
              setTimeout(() => {
                this.$router.push('/admin-login')
              }, 2000)
            } else {
              this.$message.error(response.data.message || '创建失败')
            }
          } catch (error) {
            console.error('创建管理员失败:', error)
            this.$message.error(error.response?.data?.message || '创建失败，请重试')
          } finally {
            this.loading = false
          }
        }
      })
    }
  }
}
</script>

<style scoped>
.init-admin-container {
  min-height: 100vh;
  background: linear-gradient(135deg, #409eff 0%, #667eea 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.init-admin-card {
  background: white;
  border-radius: 12px;
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.1);
  overflow: hidden;
  width: 100%;
  max-width: 450px;
}

.card-header {
  background: linear-gradient(135deg, #409eff 0%, #667eea 100%);
  color: white;
  padding: 30px;
  text-align: center;
}

.card-header h2 {
  margin: 0 0 10px 0;
  font-size: 24px;
  font-weight: 600;
}

.card-header p {
  margin: 0;
  opacity: 0.9;
  font-size: 14px;
}

.card-body {
  padding: 40px 30px;
}

.el-form-item {
  margin-bottom: 25px;
}

.el-input {
  height: 45px;
}

.el-input__inner {
  height: 45px;
  line-height: 45px;
  border-radius: 6px;
  border: 1px solid #e0e6ed;
  font-size: 14px;
}

.el-input__inner:focus {
  border-color: #409eff;
  box-shadow: 0 0 0 2px rgba(64, 158, 255, 0.2);
}

.submit-btn {
  width: 100%;
  height: 45px;
  font-size: 16px;
  font-weight: 600;
  background: linear-gradient(135deg, #409eff 0%, #667eea 100%);
  border: none;
  border-radius: 6px;
  transition: all 0.3s ease;
}

.submit-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 25px rgba(64, 158, 255, 0.3);
}

.el-form-item__label {
  font-weight: 600;
  color: #2c3e50;
}
</style> 